<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Mvc\MvcEvent;

class SuccessController extends AbstractActionController{
	protected $em;
	protected $authService;

	public function __construct(){
		$this->em = $this->getEventManager();
		$this->em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'),100);
	}

	public function preDispatch(){
        if(!$this->getAuthService()->hasIdentity())
            $this->_redirect()->toRoute(NULL,array('controller'=>'auth','action'=>'login'));
        $username = $this->getAuthService()->getStorage()->read();
        $this->layout('layout/admin');
        $this->layout()->username = $username;
    }

    public function getAuthService(){
        if(!$this->authService){
            $this->authService = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authService;
    }

	public function indexAction(){
		$result = $this->getServiceLocator()->get('AuthService')->hasIdentity();
		if(!$result){
			return $this->redirect()->toRoute(NULL,array('controller'=>'auth','action'=>'login'));
		}
		$view = new ViewModel();
		return $view;
	}
}